﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WITM.BLL;
using WITM.DAL;

namespace WITMTestApplication
{
    public partial class UsersTestForm : Form
    {
        private UserBL userBl;

        public UsersTestForm()
        {
            InitializeComponent();
            InitializeUsersGrid();
            WhereIsTheMoneyDBEntities context = new WhereIsTheMoneyDBEntities();
            context.Users.MergeOption = System.Data.Objects.MergeOption.NoTracking;
            context.SpendingCategories.MergeOption = System.Data.Objects.MergeOption.NoTracking;

            UnitOfWork unitOfWork = new UnitOfWork(context);
            userBl = new UserBL(unitOfWork);
        }
        private void InitializeUsersGrid()
        {
            dgvUsers.Columns.Add("colID", "ID");
            dgvUsers.Columns.Add("colFirstName", "First name");
            dgvUsers.Columns.Add("colLastName", "Last name");
            dgvUsers.Columns.Add("colUserName", "Username");
            dgvUsers.Columns.Add("colPassword", "Password");
        }

        private void FillUsersGrid(IEnumerable<Users> users)
        {
            dgvUsers.Rows.Clear();
            foreach (Users user in users)
            {
                dgvUsers.Rows.Add(user.UserID, user.FirstName, user.LastName, user.UserName, user.Password);                   
            }
        }

        private void btnLoadUsers_Click(object sender, EventArgs e)
        {
            IEnumerable<Users> users = userBl.GetAll();
            FillUsersGrid(users);
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            Users user = new Users()
            {
                FirstName = txtFirstName.Text,
                LastName = txtLastName.Text,
                UserName = txtUserName.Text,
                Password=  txtPassword.Text
            };
            userBl.Add(user);
            txtFirstName.Text = String.Empty;
            txtLastName.Text = String.Empty;
            txtUserName.Text = String.Empty;
            txtPassword.Text = String.Empty;
        }
    }
}
